<?php
namespace app\hotel\controller;

use app\common\controller\Hotel;
use app\common\model\Hotel\Guest;
use app\common\model\Hotel\Order;
use app\common\model\Hotel\Room;
use think\Request;

class Index extends Hotel
{
    protected $roomModel = null;
    protected $guestModel = null;
    protected $orderModel = null;
    public function __construct(Request $request)
    {
        $this->noLogin = ['index','comment','room'];
        parent::__construct($request);
        $this->roomModel = new Room();
        $this->guestModel = new Guest();
        $this->orderModel = new Order();
        $this->request->filter('trim,strip_tags');
    }
    public function index()
    {
        if($this->request->isGet())
        {
            $keywords = $this->request->get('keywords','');
            $pageSize = $this->request->get('pageSize',6);
            $page = $this->request->get('page',1);
            $where = ['name'=>['like','%'.($keywords ?? '').'%']];
            
            $list = $this->roomModel
                    ->where($where)
                    ->order('id','desc')
                    ->paginate($pageSize,$page);
            return $list;
        }
    }
    public function guest()
    {
        $busid = $this->business['id'];
        if($this->request->isGet()){
            $pageSize = $this->request->get('pageSize',6);
            $page = $this->request->get('page',0);
            $where = ['busid'=>$this->business['id']];
            if($page){
                $list = $this->guestModel
                    ->field('id,nickname,mobile,gender')
                    ->where($where)
                    ->order('id','desc')
                    ->paginate($pageSize,$page);
            }else{
                $list = $this->guestModel
                    ->field('id,nickname,mobile,gender')
                    ->where($where)
                    ->order('id','desc')
                    ->limit(6)
                    ->select();
            }
            return $list;
        }else if($this->request->isPut()){
            $id = $this->request->get('id');
            $where = ['id'=>$id,'busid'=>$busid];
            $guest = $this->guestModel->where($where)->find();
            if(!$guest)$this->error('查无住客');
            $gender = $this->request->post('gender','0');
            $mobile = $this->request->post('mobile','');
            $nickname = $this->request->post('nickname','');
            $params = [
                'gender'=>$gender,
                'mobile'=>$mobile,
                'nickname'=>$nickname,
            ];
            $result = $guest->isUpdate()->save($params);
            $result===false?$this->error('修改失败'):$this->success('修改成功');
        }else if($this->request->isPost()){
            $gender = $this->request->post('gender','0');
            $mobile = $this->request->post('mobile','');
            $nickname = $this->request->post('nickname','');
            $params = [
                'busid'=>$busid,
                'gender'=>$gender,
                'mobile'=>$mobile,
                'nickname'=>$nickname,
            ];
            $result = $this->guestModel->create($params);
            $result===false?$this->error('添加失败'):$this->success('添加成功');
        }else if($this->request->isDelete()){
            $id = $this->request->get('id');
            $where = ['id'=>$id,'busid'=>$busid];
            $guest = $this->guestModel->where($where)->find();
            if(!$guest)$this->error('查无住客');
            $result = $guest->delete();
            $result===false?$this->error('删除失败'):$this->success('删除成功');
        }
    }
    public function room()
    {
        $id = $this->request->get('id',0);
        $room = $this->roomModel->get($id);
        $where = ['roomid'=>$id,'status'=>['<','3']];
        $room['status'] = $this->orderModel->where($where)->where(['status'=>['>=','1']])->count('id')==$room['total'];
        $room?$this->success('',null,$room):$this->error('查无房间');
    }
    public function comment()
    {
        if($this->request->isGet())
        {
            $type = $this->request->get('type','');
            $roomid = $this->request->get('roomid',0);
            $where = ['status'=>4,'roomid'=>$roomid];
            if($type=='easy')
            {
                $commentList = $this->orderModel
                ->field('comment,rate,comtime')
                ->with(['business'=>function($query){
                    $query->withField('nickname,avatar');
                }])
                ->where($where)
                ->limit('3')
                ->select();
                return $commentList;
            }
        }
    }
}
